/**********************************************************************
 Name:  CreateMonthlyJointCallControl
 Purpose:Create Monthly Joint Call                                        
======================================================
History                                                            
-------                                                            
VERSION  AUTHOR            DATE              DETAIL                                
   1.0         li siheng         2010-03-16        Create          
***********************************************************************/
public class CreateMonthlyJointCallControl{
    public String msg{get;set;}     
     public Monthly_joint_call__c MonthlyJointCall{
         get{
             if(MonthlyJointCall==null){
                 MonthlyJointCall=new Monthly_joint_call__c();
             }
             return MonthlyJointCall;
         }
         set;
     }
     public PageReference save(){
         if((integer.valueof(MonthlyJointCall.year__c) < date.today().year()) ||((integer.valueof(MonthlyJointCall.year__c)  == date.today().year())&&(integer.valueof(MonthlyJointCall.month__c)  < date.today().month())))  
         {
             msg ='只能添加这个月及以后的协访计划!';
             return null;
         }              
     
         //验证是否已有记录
         list< Monthly_Joint_Call__c> MJCL=[select id from  Monthly_Joint_Call__c where year__c=:MonthlyJointCall.year__c and month__c=:MonthlyJointCall.month__c and ownerid=: userinfo.getuserid() limit 1];
         if(mjcl.size()>0){
         /*
             PageReference TempRef = new PageReference('/'+mjcl[0].id);
             TempRef.setRedirect(true);
             return TempRef;
         */
             msg = MonthlyJointCall.year__c + '年' + MonthlyJointCall.month__c + '月' + '协访计划已存在!';
             return null;
         }else{
             Monthly_Joint_Call__c insertList=new Monthly_Joint_Call__c(
                 name=userinfo.getname()+' '+MonthlyJointCall.year__c+'年'+MonthlyJointCall.month__c+'月共同拜访计划' ,
                 year__c=MonthlyJointCall.year__c,
                 month__c=MonthlyJointCall.month__c
             );
             insert insertlist;
             PageReference TempRef = new PageReference('/'+insertList.id);
             TempRef.setRedirect(true);
             return TempRef;
         }
        
         
         
     }
 
    public CreateMonthlyJointCallControl(ApexPages.StandardController controller) {
        this.controller = controller;
    }    

    private final ApexPages.StandardController controller;

    static testMethod  void test()
    {
        Profile p = [select id from profile where name='System Administrator' or name = '系统管理员' limit 1];
        User u2 = new User(alias = 'standtss', email='sdardr@testorgss.com',
        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',Title__c='TSR',
        localesidkey='en_US', profileid = p.Id,UserCity__c='广州',IsActive=true,
        UserAreaValue__c='广州区域',UserSubRegion__c='华南一区',UserRegion__c='华南区',
        timezonesidkey='America/Los_Angeles', username='stsers@testorg.com');
        insert u2;  
        
        system.runas(u2){
            Monthly_Joint_Call__c mjc=new Monthly_Joint_Call__c();
            apexPages.StandardController controller=new apexpages.standardcontroller(mjc);
            CreateMonthlyJointCallControl MJCclass=new CreateMonthlyJointCallControl(controller);
            
            MJCclass.MonthlyJointCall.month__c=string.valueof(date.today().month());
            MJCclass.MonthlyJointCall.month__c=string.valueof(date.today().year());
            mjcclass.save();
            mjcclass.save();
        }
    }

 }